package me.firstapp.spi.bbs.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.ListObject;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.bbs.JsonTopic;
import me.firstapp.common.module.bbs.JsonTopicInManage;
import me.firstapp.spi.base.impl.BaseServiceImpl;
import me.firstapp.spi.bbs.TopicService;

@Service
public class TopicServiceImpl extends BaseServiceImpl implements TopicService {

	public SingleObject<JsonTopic> addTopic(Long memberId, Long sectionId, String title, String summary,
			String content) {
		SingleObject<JsonTopic> result = new SingleObject<>();
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("title", title);
			params.put("summary", summary);
			params.put("content", content);

			Map<String, Object> parameters = new HashMap<>();
			parameters.put("memberId", memberId);
			parameters.put("sectionId", sectionId);

			JSONObject jsonObject = apiBody(parameters, "/topic/addTopic.do", "新增话题", params);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JsonTopic topic = JSONObject.toJavaObject(jsonObject.getJSONObject("object"), JsonTopic.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(topic);
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	public PageObject<JsonTopic> findTopicsPage(Integer pageNo, Integer pageSize, Long sectionId, Long memberId,
			Integer recommend, String sortParam, String sortType) {
		PageObject<JsonTopic> result = new PageObject<>();
		Map<String, Object> parameters = new HashMap<>();
		parameters.put("pageNo", pageNo);
		parameters.put("pageSize", pageSize);
		parameters.put("sectionId", sectionId);
		parameters.put("memberId", memberId);
		parameters.put("recommend", recommend);
		parameters.put("sortParam", sortParam);
		parameters.put("sortType", sortType);

		JSONObject resultObject = api(parameters, "/topic/findTopics.do", HttpMethod.GET, "分页查询话题");
		if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
			JSONObject pageJsonObject = resultObject.getJSONObject("page");
			if (pageJsonObject != null) {
				JsonPage<JsonTopic> jsonPage = new JsonPage<>();
				jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
				jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
				jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
				List<JsonTopic> topicList = new ArrayList<>();
				JSONArray jsonArray = pageJsonObject.getJSONArray("list");
				if (jsonArray != null) {
					for (int i = 0; i < jsonArray.size(); i++) {
						JSONObject _jsonObject = jsonArray.getJSONObject(i);
						if (_jsonObject != null) {
							JsonTopic topic = JSONObject.toJavaObject(_jsonObject, JsonTopic.class);
							topicList.add(topic);
						}
					}
				}
				jsonPage.setList(topicList);
				result.setPage(jsonPage);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			}
		} else {
			throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
		}
		return result;
	}

	public SingleObject<JsonTopic> findTopic(Long topicId) {
		SingleObject<JsonTopic> result = new SingleObject<JsonTopic>();
		Map<String, Object> parameters = new HashMap<>();
		parameters.put("topicId", topicId);
		JSONObject resultObject = api(parameters, "/topic/findTopicDetail.do", HttpMethod.GET, "查询话题详情");

		try {
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JsonTopic topic = JSONObject.toJavaObject(resultObject.getJSONObject("object"), JsonTopic.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(topic);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Long> findMemberTopicsCounts(Long memberId) {
		SingleObject<Long> result = new SingleObject<Long>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("memberId", memberId);
			JSONObject resultObject = api(parameters, "/topic/findMemberTopicsCounts.do", HttpMethod.GET, "查询用户发帖量");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				Long counts = resultObject.getLong("object");
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(counts);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public ListObject<JsonTopic> findSomeTopics(Long memberId, Long sectionId, Integer counts) {
		ListObject<JsonTopic> result = new ListObject<JsonTopic>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("memberId", memberId);
			parameters.put("sectionId", sectionId);
			parameters.put("counts", counts);
			JSONObject resultObject = api(parameters, "/topic/findSomeTopics.do", HttpMethod.GET, "查询用户一定数量的帖子");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JSONArray jsonArray = resultObject.getJSONArray("items");
				List<JsonTopic> results = new ArrayList<>();
				if (jsonArray != null) {
					for (int i = 0; i < jsonArray.size(); i++) {
						JSONObject jsonObject = jsonArray.getJSONObject(i);
						JsonTopic apiJsonTopic = JSONObject.toJavaObject(jsonObject, JsonTopic.class);
						results.add(apiJsonTopic);
					}
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
					result.setItems(results);
				}
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;

	}

	@Override
	public PageObject<JsonTopicInManage> findTopicsPageByParams(Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType, Long startTime, Long endTime) {
		PageObject<JsonTopicInManage> result = new PageObject<JsonTopicInManage>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("pageNo", pageNo);
			parameters.put("pageSize", pageSize);
			parameters.put("sortParam", sortParam);
			parameters.put("sortType", sortType);
			parameters.put("startTime", startTime);
			parameters.put("endTime", endTime);
			JSONObject jsonObject = apiBody(parameters, "/topic/findTopicsPageByParams.do", "分页查询话题", searchParams);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JSONObject pageJsonObject = jsonObject.getJSONObject("page");
				if (pageJsonObject != null) {
					JsonPage<JsonTopicInManage> jsonPage = new JsonPage<>();
					jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
					jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
					jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
					List<JsonTopicInManage> topicList = new ArrayList<>();
					JSONArray jsonArray = pageJsonObject.getJSONArray("list");
					if (jsonArray != null) {
						for (int i = 0; i < jsonArray.size(); i++) {
							JSONObject _jsonObject = jsonArray.getJSONObject(i);
							if (_jsonObject != null) {
								JsonTopic _topic = JSONObject.toJavaObject(_jsonObject, JsonTopic.class);
								JsonTopicInManage topic = new JsonTopicInManage(_topic);
								topicList.add(topic);
							}
						}
					}
					jsonPage.setList(topicList);
					result.setPage(jsonPage);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				}
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Object> deleteTopics(String topicIds) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("topicIds", topicIds);

			JSONObject resultJson = api(parameters, "/topic/deleteTopics.do", HttpMethod.POST, "删除话题");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonTopic> updateTopic(Long topicId, String summary, String content, Long upCounts,
			Long viewCounts, Long replyCounts, Long collectCounts, Integer recommend) {
		SingleObject<JsonTopic> result = new SingleObject<>();
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("summary", summary);
			params.put("content", content);

			Map<String, Object> parameters = new HashMap<>();
			parameters.put("topicId", topicId);
			parameters.put("upCounts", upCounts);
			parameters.put("viewCounts", viewCounts);
			parameters.put("replyCounts", replyCounts);
			parameters.put("collectCounts", collectCounts);
			parameters.put("recommend", recommend);

			JSONObject jsonObject = apiBody(parameters, "/topic/updateTopic.do", "更新话题", params);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JsonTopic topic = JSONObject.toJavaObject(jsonObject.getJSONObject("object"), JsonTopic.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(topic);
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonTopic> updateTopic(Long topicId, String summary, String content, Long upCounts,
			Long viewCounts, Long replyCounts, Long collectCounts, Integer recommend, Long apiClientId) {
		SingleObject<JsonTopic> result = new SingleObject<>();
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("summary", summary);
			params.put("content", content);

			Map<String, Object> parameters = new HashMap<>();
			parameters.put("topicId", topicId);
			parameters.put("upCounts", upCounts);
			parameters.put("viewCounts", viewCounts);
			parameters.put("replyCounts", replyCounts);
			parameters.put("collectCounts", collectCounts);
			parameters.put("recommend", recommend);
			parameters.put("apiClientId", apiClientId);

			JSONObject jsonObject = apiBody(parameters, "/batch/updateTopic.do", "更新话题", params);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JsonTopic topic = JSONObject.toJavaObject(jsonObject.getJSONObject("object"), JsonTopic.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(topic);
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

}
